package com.wmuj.controller;

import com.wmuj.annotation.LogOperation;
import com.wmuj.pojo.Result;
import com.wmuj.utils.AliyunOSSOperator;
import com.wmuj.utils.AliyunOSSProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

  /**
   * 本地磁盘存储方案 不推荐
   *
   * @param file
   * @return
   * @throws IOException @PostMapping("/upload") public Result upload(MultipartFile file) throws
   *     IOException { log.info("文件上传开始", file); // 保存文件 String originalFilename =
   *     file.getOriginalFilename(); // 新的文件名UUID String extension =
   *     originalFilename.substring(originalFilename.lastIndexOf(".")); String newFileName =
   *     UUID.randomUUID().toString() + extension;
   *     <p>file.transferTo(new File("D:/images/" + newFileName)); return Result.success(); }
   */
  @Autowired private AliyunOSSOperator aliyunOSSOperator;

  @LogOperation
  @PostMapping("/upload")
  public Result upload(MultipartFile file) throws Exception {
    log.info("文件上传开始", file);
    // 将文件交给oss存储管理
    String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
    log.info("文件上传成功，url为：{}", url);
    return Result.success(url);
  }
}
